#!/usr/bin/env python
import time
import LED_Strip as ledstrip

ledstrip.init()

def is_valid_cycles(num):
  try:
    return (int(num)>0 and int(num)<101)
  except ValueError:
    return False

def is_valid_speed(num):
    try:
        return float(num)>0
    except ValueError:
        return False

cycles = input('Please enter number of cycles [1-100]: ')
while not is_valid_cycles(cycles):
  cycles = input('Input is not valid, please reenter number of cycles: ')
cycles = int(cycles)

speed = input('\nPlease enter speed [cycles per second]: ')
while not is_valid_speed(speed):
  speed = input('Input is not valid, please reenter speed: ')
# 1 cycle (back and forth) is 14 steps
delay = 1/(14*float(speed))

print ('\nDoing '+ str(cycles) + ' cycles @ ' + speed + ' cycle(s) per second.')

start = time.time()

for k in range (0, cycles):
  # chasing LEDs 0 to 7
  for i in range (0,8):
    ledstrip.set_led(i,1)
    time.sleep(delay)
    ledstrip.set_led(i,0)
  # chasing LEDs 6 to 1
  for i in range (1,7):
    ledstrip.set_led(7-i,1)
    time.sleep(delay)
    ledstrip.set_led(7-i,0)
ledstrip.set_led(0,1)
time.sleep(delay)

stop = time.time()

# switching off all LEDs
for i in range (0,8):
  ledstrip.set_led(i,0)

print('\nDone. Duration was ' + str(round((stop-start),2)) + ' seconds.')
